import axios from 'axios'

axios.interceptors.response.use(function (response) {
    // Do something with response data
    return response;
}, function (error) {
    // Do something with response error
    console.log(error)
});

// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// axios.defaults.headers.post['Content-Type'] = 'application/json';
const APP_ID = '1P5Swvap9yHgdFkcwUHc8JddfUm';
const sougou = {

    async getToken() {

        // const url = `https://canary.speech.cogou.com/apis/auth/v1/create_token`;
        const url = `/apis/auth/v1/create_token`;

        let data = {
            appid: APP_ID,
            appkey: 'rNpqwseZbn8BsvQ+qWGYlbKbv58z/FCsTvoAW1O5fxhLYzOmGlNtQbfI7LGMPrr1hm3GpyN1UnbrWD5yDmm23w==',
            exp: '3600s'
        }
        let headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
        }
        const options = {
            method: 'POST',
            headers: headers,
            data: JSON.stringify(data),
            url,
        };
        try {
            return axios(options);
        } catch (e) {
            console.log(e);
            return null;
        }
    },
    recognize(data, token) {
        // let url = `https://api.zhiyin.sogou.com/apis/asr/v1/recognize`;
        let url = `/apis/asr/v1/recognize`;


        let headers = {
            // 'Content-Type': 'application/json',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Appid': APP_ID,
            'Authorization': `Bearer ${token}`,
            'Uuid': 'chenkai'
        }
        let paremes = {
            config: {
                encoding: 'LINEAR16',
                sample_rate_hertz: '16000',
                language_code: 'zh-cmn-Hans-CN',
            },
            audio: {
                content: data.replace(/data.+base64,/,'')
            }
        }


        const options = {
            method: 'POST',
            headers: headers,
            data: JSON.stringify(paremes),
            url,
        };

        try {
            return axios(options);
        } catch (e) {
            console.log(e);
            return null;
        }
    }
}

export {
    sougou
}